package com.temp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.temp.dto.SearchRoleDTO;
import com.temp.entity.Role;
import com.temp.mapper.RoleMapper;
import com.temp.service.RoleService;
import com.temp.vo.CustomPageVO;
import com.temp.vo.UserRolePermissionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<UserRolePermissionVO> findUserRolesAndPermissions(Long userId) {
        return roleMapper.findUserRolesAndPermissions(userId);
    }

    @Override
    public CustomPageVO<Role> findAllRoles(SearchRoleDTO roleDTO) {
        CustomPageVO<Role> customPageVO = new CustomPageVO<>();
        Integer count = Optional.ofNullable(roleMapper.countAllRole(roleDTO)).orElse(0);
        customPageVO.setTotal(count);
        if(count < 1) {
            customPageVO.setDataList(Collections.emptyList());
            return customPageVO;
        }

        List<Role> permissions = roleMapper.findAllRole(roleDTO);
        customPageVO.setDataList(permissions);
        return customPageVO;
    }
}
